//MIT License
//
//Copyright(c) 2020 Goodman Tao
//
//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files(the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions :
//
//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.
//
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.


#if defined(_MSC_VER)
    #define _LB_2_MULTITHREAD_API __declspec(dllexport)
#endif

#if defined(__GNUC__)
    #define _LB_2_MULTITHREAD_API
#endif

#include "_LB_2_Multithread.h"

#include <stdlib.h>
#include "tinycthread.h"


typedef struct
{
    cnd_t *condVar;
}Fld;

static void Clear(void *pParams)
{
    ConditionVariable *pThis = pParams;

    Fld *pFld = pThis->Fld;

    cnd_destroy(pFld->condVar);
    free(pFld->condVar);
}

static void Init(void *_pThis, va_list *pvlArgs)
{
    ConditionVariable *pThis = _pThis;

    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);
    
    Fld *pFld = pThis->Fld;

    pFld->condVar = malloc(sizeof(cnd_t));
    ConcurrenceReturnValue ret = cnd_init(pFld->condVar);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void Signal(void *_pThis, va_list *pvlArgs)
{
    ConditionVariable *pThis = _pThis;

    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);

    Fld *pFld = pThis->Fld;

    ConcurrenceReturnValue ret = cnd_signal(pFld->condVar);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void Broadcast(void *_pThis, va_list *pvlArgs)
{
    ConditionVariable *pThis = _pThis;

    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);

    Fld *pFld = pThis->Fld;

    ConcurrenceReturnValue ret = cnd_broadcast(pFld->condVar);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

typedef struct
{
    mtx_t *mtx;
}MutexFld;

static void Wait(void *_pThis, va_list *pvlArgs)
{
    ConditionVariable *pThis = _pThis;

    Mutex *mtx = va_arg(*pvlArgs, void *);
    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);

    Fld *pFld = pThis->Fld;
    MutexFld *mtxFld = mtx->Fld;
    ConcurrenceReturnValue ret = cnd_wait(pFld->condVar, mtxFld->mtx);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

static void TimedWait(void *_pThis, va_list *pvlArgs)
{
    ConditionVariable *pThis = _pThis;

    Mutex *mtx = va_arg(*pvlArgs, void *);
    const struct timespec *ts = va_arg(*pvlArgs, void *);
    ConcurrenceReturnValue *pRetVal = va_arg(*pvlArgs, void *);

    Fld *pFld = pThis->Fld;
    MutexFld *mtxFld = mtx->Fld;
    ConcurrenceReturnValue ret = cnd_timedwait(pFld->condVar, mtxFld->mtx, ts);
    if (pRetVal)
    {
        *pRetVal = ret;
    }
}

__CONSTRUCTOR(ConditionVariable)
{
    return __Inst(__TYPE(ConditionVariable), sizeof(Fld), Clear, 5, 0,
        __METHOD(Init),
        __METHOD(Signal),
        __METHOD(Broadcast),
        __METHOD(Wait),
        __METHOD(TimedWait));
}
